<?php
App::uses ( 'AuthComponent', 'Controller/Component' );
class UsersController extends AppController {
	public $helpers = array (
			'Html',
			'Form',
			'Session' 
	);
	public function login() {
		if ($this->request->is ( 'post' )) {
			if ($this->Auth->login ()) {
				// add role depend on type of user, HARDCODE, need to read from config file
				$sessionUser = $this->Session->read ( 'Auth.User' );
				$sessionUser ['role'] = $this->getRole ( $sessionUser ['type'] );
				// add modified user into session again
				$this->Auth->login ( $sessionUser );
				
				$this->Session->setFlash ( 'Login success! Welcome', 'default', array (
						'class' => 'alert alert-success' 
				) );
				// return $this->redirect($this->Auth->redirect());
				
				return $this->redirect ( array (
						'controller' => 'posts',
						'action' => 'index' 
				) );
			}
			$this->Session->setFlash ( 'Invalid username or password, try again', 'default', array (
					'class' => 'alert alert-warning' 
			) );
		}
	}
	
	/*
	 * public function login() { if ($this->request->is ( 'post' )) { if (! $this->request->data ['username']) { $this->Session->setFlash ( 'Please enter username!', 'default', array ( 'class' => 'alert alert-danger' ) ); return; } if (! $this->request->data ['password']) { $this->Session->setFlash ( 'Please enter password!', 'default', array ( 'class' => 'alert alert-danger' ) ); return; } $username = $this->request->data ['username']; $password = $this->request->data ['password']; $user = $this->User->find ( 'first', array ( 'conditions' => array ( 'User.username' => $username ) ) ); if (! $user) { $this->Session->setFlash ( 'Username or password is incorrect!', 'default', array ( 'class' => 'alert alert-danger' ) ); return; } if ($user ['User'] ['password'] != AuthComponent::password ( $password )) { $this->Session->setFlash ( 'Username or password is incorrect!', 'default', array ( 'class' => 'alert alert-danger' ) ); return; } else { //remove unnecessary elements unset($user['User']['password']); unset($user['User']['created']); unset($user['User']['modified']); unset($user['Group']); //add role to the logined user $user['User']['role'] = $this->getRole($user['User']['type']); //echo '<pre>' ; print_r($user); exit; $this->Auth->login ( $user ['User'] ); $this->Session->write ( 'userLogin', $user ); $sessionUser = $this->Session->read ( 'Auth.User'); //echo '<pre> session '; print_r($sessionUser); echo 'login'; exit; $this->Session->setFlash ( 'Login success! Welcome ' . $user ['User'] ['fullname'] . '!', 'default', array ( 'class' => 'alert alert-success' ) ); return $this->redirect ( array ( 'controller' => 'posts', 'action' => 'index' ) ); } } }
	 */
	private function getRole($type) {
		if ($type == 1) {
			return 'admins';
		} else if ($type == 2) {
			return 'users';
		} else {
			return 'guesses';
		}
	}
	public function logout() {
		if (! $this->Session->check ( 'Auth.User' )) {
			$this->Session->setFlash ( 'You are not logged in. Please login again!', 'default', array (
					'class' => 'alert alert-danger' 
			) );
			return $this->render ( "/users/login" );
		} else {
			
			$this->Auth->logout ();
			$this->Session->delete ( 'userLogin' );
			$this->Session->setFlash ( 'Logout success, Goodbye', 'default', array (
					'class' => 'alert alert-success' 
			) );
			return $this->redirect ( array (
					'controller' => 'posts',
					'action' => 'index' 
			) );
		}
	}
	public function logout2() {
		$this->Session->setFlash ( 'Goodbye', 'default', array (
				'class' => 'alert alert-info' 
		) );
		$this->redirect ( $this->Auth->logout () );
	}
	
	/**
	 * Method register
	 */
	public function register() {
		// check login
		if ($this->Session->check ( 'Auth.User' )) {
			$this->Session->setFlash ( 'You already have an account!!', 'default', array (
					'class' => 'alert alert-danger' 
			) );
			return $this->redirect ( array (
					'controller' => 'posts',
					'action' => 'index' 
			) );
		}
		
		// validate input
		if ($this->request->is ( 'post' )) {
			$username = $this->request->data ['username'];
			$admin = $this->User->find ( 'first', array (
					'conditions' => array (
							'User.username' => $username 
					) 
			) );
			if ($admin) {
				$this->Session->setFlash ( 'Username had been used!!!', 'default', array (
						'class' => 'alert alert-warning' 
				) );
				return $this->render ( '/users/register' );
			}
			$pass = $this->request->data ['password'];
			$confirm = $this->request->data ['confirmPassword'];
			if ($pass != $confirm) {
				$this->Session->setFlash ( 'Confirm password is not correct!!!', 'default', array (
						'class' => 'alert alert-warning' 
				) );
				return $this->render ( '/users/register' );
			}
			// save to db
			$this->User->set ( array (
					'username' => $this->request->data ['username'],
					'password' => AuthComponent::password ( $this->request->data ['password'] ),
					'fullname' => $this->request->data ['fullname'],
					'group_id' => $this->request->data ['group_id'] 
			) );
			if ($this->User->save ()) {
				$this->Session->setFlash ( 'Register successfully. You can login now', 'default', array (
						'class' => 'alert alert-success' 
				) );
				return $this->redirect ( array (
						'controller' => 'posts',
						'action' => 'index' 
				) );
			}
			$this->Session->setFlash ( 'Register failed!!!', 'default', array (
					'class' => 'alert alert-warning' 
			) );
			return $this->render ( '/users/register' );
		}
	}
	public function beforeFilter() {
		parent::beforeFilter ();
		$this->Auth->allow ( 'login', 'logout', 'logout2', 'initDB', 'register' ); // We can remove this line after we're finished
	}
	
	// init aros_acos for acl database
	public function initDB() {
		$group = $this->User->Group;
		
		// Allow admins to everything
		$group->id = 4;
		$this->Acl->allow ( $group, 'controllers' );
		
		// allow managers to posts and widgets
		$group->id = 5;
		$this->Acl->deny ( $group, 'controllers' );
		$this->Acl->allow ( $group, 'controllers/Posts' );
		$this->Acl->allow ( $group, 'controllers/AdminPosts' );
		$this->Acl->allow ( $group, 'controllers/Search' );
		$this->Acl->allow ( $group, 'controllers/Comments' );
		// $this->Acl->allow($group, 'controllers/Widgets');
		
		// allow users to only add and edit on posts and widgets
		$group->id = 6;
		$this->Acl->deny ( $group, 'controllers' );
		// $this->Acl->allow($group, 'controllers/Posts/index');
		// $this->Acl->allow($group, 'controllers/Posts/detail');
		$this->Acl->allow ( $group, 'controllers/Search' );
		$this->Acl->allow ( $group, 'controllers/Comments/add' );
		// $this->Acl->allow($group, 'controllers/Widgets/add');
		// $this->Acl->allow($group, 'controllers/Widgets/edit');
		
		// allow basic users to log out
		$this->Acl->allow ( $group, 'controllers/users/logout' );
		
		// we add an exit to avoid an ugly "missing views" error message
		echo "all done";
		exit ();
	}
}